<div id="required_fields_message"><?php echo $this->lang->line('items_edit_fields_you_want_to_update'); ?></div>
<ul id="error_message_box"></ul>
<?php
echo form_open('items/bulk_update/',array('id'=>'item_form'));
?>
<fieldset id="item_basic_info">
<legend><?php echo $this->lang->line("items_basic_information"); ?></legend>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_name').':', 'name',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'name',
		'id'=>'name')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_category').':', 'category',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'category',
		'id'=>'category')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_supplier').':', 'supplier',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_dropdown('supplier_id', $suppliers, '');?>
	</div>
</div>


<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_cost_price').':', 'cost_price',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'cost_price',
		'size'=>'8',
		'id'=>'cost_price')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_unit_price').':', 'unit_price',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'unit_price',
		'size'=>'8',
		'id'=>'unit_price')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_percentage_profit').':', 'percentage_profit',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'percentage_profit',
		'size'=>'8',
		'id'=>'percentage_profit')
	);?>%
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_tax_1').':', 'tax_percent_1',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'tax_names[]',
		'id'=>'tax_name_1',
		'size'=>'8',
		'value'=> isset($item_tax_info[0]['name']) ? $item_tax_info[0]['name'] : $this->lang->line('items_sales_tax'))
	);?>
	</div>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'tax_percents[]',
		'id'=>'tax_percent_name_1',
		'size'=>'3',
		'value'=> isset($item_tax_info[0]['percent']) ? $item_tax_info[0]['percent'] : '')
	);?>
	%
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_tax_2').':', 'tax_percent_2',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'tax_names[]',
		'id'=>'tax_name_2',
		'size'=>'8',
		'value'=> isset($item_tax_info[1]['name']) ? $item_tax_info[1]['name'] : '')
	);?>
	</div>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'tax_percents[]',
		'id'=>'tax_percent_name_2',
		'size'=>'3',
		'value'=> isset($item_tax_info[1]['percent']) ? $item_tax_info[1]['percent'] : '')
	);?>
	%
	</div>
</div>
<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_reorder_level').':', 'reorder_level',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'reorder_level',
		'id'=>'reorder_level')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_location').':', 'location',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_input(array(
		'name'=>'location',
		'id'=>'location')
	);?>
	</div>
</div>

<div class="field_row clearfix">	
<?php echo form_label($this->lang->line('items_description').':', 'description',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_textarea(array(
		'name'=>'description',
		'id'=>'description',
		'rows'=>'5',
		'cols'=>'17')		
	);?>
	</div>
</div>
<!--
<div class="field_row clearfix">

<?php echo form_label($this->lang->line('items_allow_alt_desciption').':', 'allow_alt_description',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_dropdown('allow_alt_description', $allow_alt_desciption_choices);?>

	</div>

</div>



<div class="field_row clearfix">

<?php echo form_label($this->lang->line('items_is_serialized').':', 'is_serialized',array('class'=>'wide')); ?>
	<div class='form_field'>
	<?php echo form_dropdown('is_serialized', $serialization_choices);?>

	</div>

</div>
-->
<?php
echo form_submit(array(
	'name'=>'submit',
	'id'=>'submit',
	'value'=>$this->lang->line('common_submit'),
	'class'=>'submit_button float_right')
);
?>
</fieldset>
<?php 
echo form_close();
?>
<script type='text/javascript'>

//validation and submit handling
$(document).ready(function()
{	
	$("#category").autocomplete("<?php echo site_url('items/suggest_category');?>",{max:100,minChars:0,delay:10});
    $("#category").result(function(event, data, formatted)
    {
    });
	$("#category").search();
	
	/** Check and calculate for profit % or unit cost **/
	
	$("#cost_price").bind('focusout',function(){
		var unit_price = $("#unit_price").val();
		var percentage_profit = $("#percentage_profit").val();
		if(unit_price.length > 0){
			$("#unit_price").keyup();
		}
		else if(percentage_profit.length > 0){
			$("#percentage_profit").keyup();
		}
		else{
			alert('Unit Price or Percentage Profit is reqiured');
		}
	});
	
	$("#unit_price").keyup(function(){
		var unit_price = $(this).val();
		var cost_price = $("#cost_price").val();
		var res = checkCostUnitPrice(cost_price, unit_price, 'Unit Price must be a number');
		if(res.status){
			calcPercentProfit(cost_price, unit_price);
		}
		else{
			alert(res.msg);
		}
	});
	
	$("#percentage_profit").keyup(function(){
		var percentage_profit = $(this).val();
		var cost_price = $("#cost_price").val();
		var res = checkCostUnitPrice(cost_price, percentage_profit, 'Unit Price must be a number');
		if(res.status){
			calcUnitPrice(cost_price, percentage_profit);
		}
		else{
			alert(res.msg);
		}
	});
	
	function calcPercentProfit(cost_priceP, unit_priceP){
		var cost_price = parseFloat(cost_priceP);
		var unit_price = parseFloat(unit_priceP);
		var pP = ((unit_price - cost_price) / cost_price) * 100 ;
		$("#percentage_profit").val(pP.toFixed(2));
	}
	
	function calcUnitPrice(cost_priceP, percentage_profitP){
		var cost_price = parseFloat(cost_priceP);
		var percentage_profit = parseFloat(percentage_profitP);
		var uPP = ((percentage_profit/100) * cost_price);
		var uP = uPP + cost_price;
		$("#unit_price").val(uP.toFixed(2));
	}
	
	function IsNumeric(input){
		return (input - 0) == input && input.length > 0;
	}
	
	function checkCostUnitPrice(cost_price, other_price, txt){
		var retrunArr = {status:true,msg:''};
		if(!(cost_price.length > 0)){
			retrunArr.status = false;
			retrunArr.msg = 'Please Enter Cost Price';
			return retrunArr;
		}
		if(!IsNumeric(cost_price)){
			retrunArr.status = false;
			retrunArr.msg = 'Cost Price must be a number';
			return retrunArr;
		}
		if(!IsNumeric(other_price)){
			retrunArr.status = false;
			retrunArr.msg = txt;
			return retrunArr;
		}
		return retrunArr;
	}	
	
	function checkAll(cost_price, unit_price, percentage_profit){
		var retrunArr = {status:true,msg:''};
		if(!(cost_price.length > 0)){
			retrunArr.status = false;
			retrunArr.msg = 'Please Enter Cost Price';
			return retrunArr;
		}
		if(!(unit_price.length > 0)){
			retrunArr.status = false;
			retrunArr.msg = 'Please Enter Unit Price';
			return retrunArr;
		}
		if(!(percentage_profit.length > 0)){
			retrunArr.status = false;
			retrunArr.msg = 'Please Enter Percentage Profit';
			return retrunArr;
		}
		
		return retrunArr;
	}
		
	
	$('#item_form').validate({
		submitHandler:function(form)
		{
			var pass = true;
			
			if($("#cost_price").val().lenght > 0){
				var res = checkAll($("#cost_price").val(), $("#unit_price").val(), $("#percentage_profit").val());
				pass = res.status;
			}
			if($("#unit_price").val().lenght > 0){
				var res = checkAll($("#cost_price").val(), $("#unit_price").val(), $("#percentage_profit").val());
				pass = res.status;
			}
			if($("#percentage_profit").val().lenght > 0){
				var res = checkAll($("#cost_price").val(), $("#unit_price").val(), $("#percentage_profit").val());
				pass = res.status;
			}
			
			if(pass){
				if(confirm("<?php echo $this->lang->line('items_confirm_bulk_edit') ?>"))
				{
					//Get the selected ids and create hidden fields to send with ajax submit.
					var selected_item_ids=get_selected_values();
					for(k=0;k<selected_item_ids.length;k++)
					{
						$(form).append("<input type='hidden' name='item_ids[]' value='"+selected_item_ids[k]+"' />");
					}
					
					
					$(form).ajaxSubmit({
					success:function(response)
					{
						tb_remove();
						post_bulk_form_submit(response);
					},
					dataType:'json'
					});
				}
			}
			else{
				alert(res.msg);
			}
		},
		errorLabelContainer: "#error_message_box",
 		wrapper: "li",
		rules: 
		{
			cost_price:
			{
				number:true
			},
			unit_price:
			{
				number:true
			},
			percentage_profit:
			{
				number:true
			},
			tax_percent:
			{
				number:true
			},
			quantity:
			{
				number:true
			},
			reorder_level:
			{
				number:true
			}
   		},
		messages: 
		{
			cost_price:
			{
				number:"<?php echo $this->lang->line('items_cost_price_number'); ?>"
			},
			unit_price:
			{
				number:"<?php echo $this->lang->line('items_unit_price_number'); ?>"
			},
			percentage_profit:
			{
				number:"<?php echo $this->lang->line('items_percentage_profit_number'); ?>"
			},
			tax_percent:
			{
				number:"<?php echo $this->lang->line('items_tax_percent_number'); ?>"
			},
			quantity:
			{
				number:"<?php echo $this->lang->line('items_quantity_number'); ?>"
			},
			reorder_level:
			{
				number:"<?php echo $this->lang->line('items_reorder_level_number'); ?>"
			}

		}
	});
});
</script>